package com.hsb.controller;

import com.hsb.entity.User;
import com.hsb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //增加一条记录
    @PostMapping("/insert/{userName}/{userPwd}/{userEmail}")
    public Boolean insert(@PathVariable("userName") String userName,
                          @PathVariable("userPwd") String userPwd,
                          @PathVariable("userEmail") String userEmail) {
        int count = userService.insert(new User(userName, userPwd, userEmail));
        return count > 0;
    }

    //根据id删除记录
    @DeleteMapping("/delete/{id}")
    public Boolean deleteById(@PathVariable Integer id) {
        int count = userService.deleteById(id);
        return count > 0;
    }

    //根据id查询记录
    @GetMapping("/query/{id}")
    public User queryById(@PathVariable Integer id) {
        return userService.queryById(id);
    }

    //查询所有记录
    @GetMapping("/queryAll")
    public List<User> queryAll() {
        return userService.queryAll();
    }

    //根据id修改记录
    @PutMapping("/update/{id}/{userName}/{userPwd}/{userEmail}")
    public Boolean update(@PathVariable("id") Integer id,
                          @PathVariable("userName") String userName,
                          @PathVariable("userPwd") String userPwd,
                          @PathVariable("userEmail") String userEmail) {
        int count = userService.updateById(new User(id, userName, userPwd, userEmail));
        return count > 0;
    }
}
